import request from "@/utils/request";
import { FoodCategoryVO } from "@/types/store";

/**
 * 获取商家店铺列表
 */
export function getMerchantStores() {
  return request({
    url: "/api/merchant/stores",
    method: "get",
  });
}

/**
 * 获取店铺详情
 * @param id 店铺ID
 */
export function getStoreDetail(id: string) {
  return request({
    url: `/api/merchant/stores/${id}`,
    method: "get",
  });
}

/**
 * 创建店铺
 * @param data 店铺数据
 */
export function createStore(data: any) {
  return request({
    url: "/api/merchant/stores",
    method: "post",
    data,
  });
}

/**
 * 更新店铺信息
 * @param id 店铺ID
 * @param data 店铺数据
 */
export function updateStore(id: string, data: any) {
  return request({
    url: `/api/merchant/stores/${id}`,
    method: "put",
    data,
  });
}

/**
 * 更新店铺状态
 * @param id 店铺ID
 * @param data 新的状态信息
 */
export function updateStoreStatus(id: string, data: any) {
  console.log("调用updateStoreStatus API，参数:", { id, data });
  return request({
    url: `/api/merchant/stores/${id}/status`,
    method: "put",
    data,
  });
}

/**
 * 上传商家头像
 * @param storeId 店铺ID
 * @param data 文件数据
 */
export function uploadStoreAvatar(storeId: string, data: FormData) {
  return request({
    url: `/api/merchant/stores/${storeId}/avatar`,
    method: "post",
    data,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
}

/**
 * 删除商家头像
 * @param storeId 店铺ID
 * @param data 删除的文件对应的url
 */
export function deleteStoreAvatar(storeId: string, data: { url: string }) {
  return request({
    url: `/api/merchant/stores/${storeId}/avatar`,
    method: "delete",
    data,
  });
}

/**
 * 获取店铺促销列表
 * @param storeId 店铺ID
 */
export function listPromotions(storeId: string) {
  return request({
    url: `/api/merchant/stores/${storeId}/promotions`,
    method: "get",
  });
}

/**
 * 创建促销活动
 * @param storeId 店铺ID
 * @param data 新促销活动信息
 */
export function createPromotion(storeId: string, data: any) {
  return request({
    url: `/api/merchant/stores/${storeId}/promotions`,
    method: "post",
    data,
  });
}

/**
 * 更新促销活动
 * @param storeId 店铺ID
 * @param id 促销活动ID
 * @param data 更新后的促销活动信息
 */
export function updatePromotion(storeId: string, id: string, data: any) {
  return request({
    url: `/api/merchant/stores/${storeId}/promotions/${id}`,
    method: "put",
    data,
  });
}

/**
 * 删除促销活动
 * @param storeId 店铺ID
 * @param id 促销活动ID
 */
export function deletePromotion(storeId: string, id: string) {
  return request({
    url: `/api/merchant/stores/${storeId}/promotions/${id}`,
    method: "delete",
  });
}

/**
 * 获取商品列表
 * @param storeId 店铺ID
 * @param params 查询参数 (categoryId, status, page, size)
 */
export function listFoods(
  storeId: string,
  params?: {
    categoryId?: number;
    status?: string;
    page?: number;
    size?: number;
  }
) {
  return request({
    url: `/api/merchant/stores/${storeId}/foods`,
    method: "get",
    params,
  });
}

/**
 * 获取店铺订单统计数据
 * @param storeId 店铺ID
 * @param params 查询参数 (startDate, endDate)
 */
export function getStoreOrderStats(
  storeId: string,
  params?: { startDate?: string; endDate?: string }
) {
  return request({
    url: `/api/merchant/stores/${storeId}/statistics/orders`,
    method: "get",
    params,
  });
}

/**
 * 获取店铺评论统计数据
 * @param storeId 店铺ID
 * @param params 查询参数 (startDate, endDate)
 */
export function getStoreReviewStats(
  storeId: string,
  params?: { startDate?: string; endDate?: string }
) {
  return request({
    url: `/api/merchant/stores/${storeId}/statistics/reviews`,
    method: "get",
    params,
  });
}

/**
 * 获取店铺热门商品
 * @param storeId 店铺ID
 * @param params 查询参数 (limit)
 */
export function getStoreTopProducts(
  storeId: string,
  params?: { limit?: number }
) {
  return request({
    url: `/api/merchant/stores/${storeId}/statistics/top-products`,
    method: "get",
    params,
  });
}

/**
 * 添加商品
 * @param storeId 店铺ID
 * @param data 新商品信息
 */
export function createFood(storeId: string, data: any) {
  return request({
    url: `/api/merchant/stores/${storeId}/foods`,
    method: "post",
    data,
  });
}

/**
 * 获取单个商品信息
 * @param storeId 店铺ID
 * @param foodId 商品ID
 */
export function getFoodByStoreIdAndFoodId(storeId: string, foodId: string) {
  return request({
    url: `/api/merchant/stores/${storeId}/foods/${foodId}`,
    method: "get",
  });
}

/**
 * 更新商品信息
 * @param storeId 店铺ID
 * @param foodId 商品ID
 * @param data 更新后的商品信息
 */
export function updateFood(storeId: string, foodId: string, data: any) {
  return request({
    url: `/api/merchant/stores/${storeId}/foods/${foodId}`,
    method: "put",
    data,
  });
}

/**
 * 删除商品
 * @param storeId 店铺ID
 * @param foodId 商品ID
 */
export function deleteFood(storeId: string, foodId: string) {
  return request({
    url: `/api/merchant/stores/${storeId}/foods/${foodId}`,
    method: "delete",
  });
}

/**
 * 更改商品状态
 * @param storeId 店铺ID
 * @param foodId 商品ID
 * @param data 新的状态信息
 */
export function updateFoodStatus(storeId: string, foodId: string, data: any) {
  return request({
    url: `/api/merchant/stores/${storeId}/foods/${foodId}/status`,
    method: "put",
    data,
  });
}

/**
 * 上传商品图片
 * @param storeId 店铺ID
 * @param data 文件数据
 */
export function uploadFoodImage(storeId: string, data: FormData) {
  console.log(`准备上传商品图片到店铺 ${storeId}`);

  // 检查FormData内容
  try {
    console.log("FormData内容:");
    for (const pair of data.entries()) {
      console.log(`${pair[0]}: ${pair[1]}`);
    }
  } catch (e) {
    console.error("无法读取FormData内容:", e);
  }

  return request({
    url: `/api/merchant/stores/${storeId}/foods/dishes`,
    method: "post",
    data,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  })
    .then((response) => {
      console.log("上传商品图片响应:", response);
      return response;
    })
    .catch((error) => {
      console.error("上传商品图片失败:", error);
      throw error;
    });
}

/**
 * 删除商品图片
 * @param storeId 店铺ID
 * @param data 删除的文件对应的url
 */
export function deleteFoodImage(storeId: string, data: { url: string }) {
  return request({
    url: `/api/merchant/stores/${storeId}/foods/dishes`,
    method: "delete",
    data,
  });
}

/**
 * 获取商品分类列表
 * @param storeId 店铺ID
 */
export function getCategories(storeId: string): Promise<FoodCategoryVO[]> {
  // 从店铺详情API获取商品分类列表
  return request({
    url: `/api/stores/${storeId}`,
    method: "get",
  }).then((res) => {
    // 从店铺详情中提取商品分类列表
    const storeDetail = res.data?.data || res.data;
    return storeDetail?.categories || [];
  });
}

/**
 * 添加商品分类
 * @param storeId 店铺ID
 * @param data 商品分类信息
 */
export function addCategory(storeId: string, data: any) {
  // 注意：API文档中没有明确提供添加商品分类的接口
  // 这里假设使用商家API添加商品分类
  return request({
    url: `/api/merchant/stores/${storeId}/categories`,
    method: "post",
    data,
  });
}

/**
 * 更新商品分类
 * @param storeId 店铺ID
 * @param id 分类ID
 * @param data 商品分类信息
 */
export function updateCategory(storeId: string, id: string, data: any) {
  // 注意：API文档中没有明确提供更新商品分类的接口
  // 这里假设使用商家API更新商品分类
  return request({
    url: `/api/merchant/stores/${storeId}/categories/${id}`,
    method: "put",
    data,
  });
}

/**
 * 删除商品分类
 * @param storeId 店铺ID
 * @param id 分类ID
 */
export function deleteCategory(storeId: string, id: string) {
  // 注意：API文档中没有明确提供删除商品分类的接口
  // 这里假设使用商家API删除商品分类
  return request({
    url: `/api/merchant/stores/${storeId}/categories/${id}`,
    method: "delete",
  });
}

/**
 * 获取订单列表
 * @param storeId 店铺ID
 * @param params 查询参数 (status, page, size)
 */
export function getOrders(
  storeId: string,
  params?: { status?: string; page?: number; size?: number }
) {
  return request({
    url: `/api/merchant/orders/${storeId}`,
    method: "get",
    params,
  });
}

/**
 * 店铺更新订单状态
 * @param storeId 店铺ID
 * @param orderId 订单ID
 * @param data 新的状态信息 { orderStatus: string }
 */
export function updateOrderStatus(
  storeId: string,
  orderId: string,
  data: { orderStatus: string }
) {
  return request({
    url: `/api/merchant/orders/${storeId}/${orderId}/status`,
    method: "put",
    data,
  });
}

/**
 * 店铺根据订单id和用户id获取订单详情
 * @param storeId 店铺ID
 * @param userId 用户ID
 * @param id 订单ID
 */
export function getDetailOrders(storeId: string, id: string) {
  return request({
    url: `/api/merchant/orders/${storeId}/orderDetail?id=${id}`,
    method: "get",
  });
}

/**
 * 获取店铺评价列表
 * @param storeId 店铺ID
 * @param params 查询参数 (storeRating, page, size)
 */
export function listReviews(
  storeId: string,
  params?: { storeRating?: number; page?: number; size?: number }
) {
  return request({
    url: `/api/merchant/stores/${storeId}/reviews`,
    method: "get",
    params,
  });
}

/**
 * 获取店铺统计数据
 * @param storeId 店铺ID
 * @param params 查询参数 (startDate, endDate)
 */
export function getPlatformStatistics(
  storeId: string,
  params?: { startDate?: string; endDate?: string }
) {
  return request({
    url: `/api/merchant/stores/statics/${storeId}`,
    method: "get",
    params,
  });
}
